//
//  SLBoatBackMsgViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/6/3.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLBackMsgViewController.h"
#import "STPopup.h"
#import "SLNetWork.h"


@interface SLBackMsgViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UIButton *confirmBtn;

@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UIButton *noteButton;

@property (nonatomic, strong) UITextView *noteTextView;

@property (nonatomic, strong) UILabel *firstLB;

@property (nonatomic, strong) UILabel *secondLB;

@property (nonatomic, strong) UILabel *thridLB;

@property (nonatomic, strong) UIButton *firstBtn;

@property (nonatomic, strong) UIButton *secondBtn;

@property (nonatomic, strong) UIButton *thridBtn;

//@property (nonatomic, strong) NSString *content;

@property (nonatomic, assign) NSInteger backMsgType;

@property (nonatomic, strong) NSString *backMsgId;

@end

@implementation SLBackMsgViewController


- (instancetype)init{
  if (self = [super init]) {
    self.contentSizeInPopup = CGSizeMake(300*MIX_W, 300*MIX_W);
    self.landscapeContentSizeInPopup = CGSizeMake(300*MIX_W, 300*MIX_W);
  }
  return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
  [self setUpUI];
  [self addConstrains];
  [self addObservers];
  
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewEndEdit)];
  [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view.
}

- (void)addObservers{
  @weakify(self)
  [[RACObserve(self.firstBtn, selected) filter:^BOOL(NSNumber *value) {
    return value.boolValue;
  }] subscribeNext:^(id x) {
      @strongify(self)
    self.secondBtn.selected = NO;
    self.thridBtn.selected = NO;
    self.noteButton.selected = NO;
  }];
  
  [[RACObserve(self.secondBtn, selected) filter:^BOOL(NSNumber *value) {
    return value.boolValue;
  }] subscribeNext:^(id x) {
    @strongify(self)
    self.firstBtn.selected = NO;
    self.thridBtn.selected = NO;
    self.noteButton.selected = NO;
  }];
  
  
  [[RACObserve(self.thridBtn, selected) filter:^BOOL(NSNumber *value) {
    return value.boolValue;
  }] subscribeNext:^(id x) {
    @strongify(self)
    self.firstBtn.selected = NO;
    self.secondBtn.selected = NO;
    self.noteButton.selected = NO;
  }];
  
  
  [[RACObserve(self.noteButton, selected) filter:^BOOL(NSNumber *value) {
    @strongify(self)
    self.noteTextView.editable = [value boolValue];
    return value.boolValue;
  }] subscribeNext:^(id x) {
    @strongify(self)
    self.firstBtn.selected = NO;
    self.secondBtn.selected = NO;
    self.thridBtn.selected = NO;
    [self.noteTextView resignFirstResponder];
  }];
  
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
  if (range.location > 200){
    [SVProgressHUD showImage:nil status:@"已达输入上限"];
    return  NO;
  }
  
  return YES;
}

- (void)addConstrains{
  [self.firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(15);
    make.top.equalTo(self.mas_topLayoutGuideBottom).offset(10);
    make.size.mas_equalTo(CGSizeMake(30*MIX_W, 30*MIX_W));
  }];
  
  [self.firstLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.firstBtn.mas_right).offset(2);
    make.right.equalTo(self.view);
    make.centerY.equalTo(self.firstBtn);
  }];
  
  [self.secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.firstBtn.mas_bottom).offset(10);
    make.size.left.equalTo(self.firstBtn);
  }];
  
  [self.secondLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view);
    make.left.equalTo(self.firstLB);
    make.centerY.equalTo(self.secondBtn);
  }];
  
  [self.thridBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.secondBtn.mas_bottom).offset(10);
    make.size.left.equalTo(self.firstBtn);
  }];
  
  [self.thridLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view);
    make.left.equalTo(self.firstLB);
    make.centerY.equalTo(self.thridBtn);
  }];
  
  [self.noteButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.thridBtn.mas_bottom).offset(10);
    make.size.left.equalTo(self.firstBtn);
  }];
  
  UILabel *label = [UILabel labelWithFontSize:15 textColor:[UIColor blackColor] text:@"其他 :"];
  [self.view addSubview:label];
  [label mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.noteButton);
    make.left.equalTo(self.firstLB);
  }];
  
  [self.noteTextView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(label.mas_right);
    make.top.equalTo(self.noteButton);
    make.right.equalTo(self.view).offset(-5);
    make.height.equalTo(@100);
  }];
  
  [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.view).offset(-10);
    make.left.equalTo(self.firstBtn).offset(10);
  }];
  
  [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.centerY.equalTo(self.confirmBtn);
    make.right.equalTo(self.noteTextView).offset(-10);
  }];
  
  
}

- (void)setUpUI{
  [self.view addSubview:self.firstBtn];
  [self.view addSubview:self.firstLB];
  [self.view addSubview:self.secondBtn];
  [self.view addSubview:self.secondLB];
  [self.view addSubview:self.thridBtn];
  [self.view addSubview:self.thridLB];
  [self.view addSubview:self.noteButton];
  [self.view addSubview:self.noteTextView];
  [self.view addSubview:self.confirmBtn];
  [self.view addSubview:self.cancelBtn];
}

#pragma mark - Private Methods

- (void)viewEndEdit{
  [self.view endEditing:YES];
}

static NSString *content;

- (void)choeseFirst:(UIButton *)btn{
  self.firstBtn.selected = !btn.selected;
  if (self.firstBtn.selected) {
    content = self.firstLB.text;
  }
  
}

- (void)choeseSecond:(UIButton *)btn{
  self.secondBtn.selected = !btn.selected;
  if (self.secondBtn.selected) {
    content = self.secondLB.text;
  }
}

- (void)choeseThird:(UIButton *)btn{
  self.thridBtn.selected = !btn.selected;
  if (self.thridBtn.selected) {
    content = self.thridLB.text;
  }
}

- (void)choeseNote:(UIButton *)btn{
  self.noteButton.selected = !btn.selected;
  if (self.noteButton.selected) {
    [self.noteTextView becomeFirstResponder];
  }
  
}

static BOOL isNoteBtn;

- (void)confirmAction{
  
  if ([self vailToNext]) {
    if (isNoteBtn) {
      content = self.noteTextView.text;
    }
    [self backMsg];
  }

}


- (void)backMsg{
  NSString *uid = [DEFAULTS objectForKey:loginInfo][@"id"];
  
  @weakify(self)

  if (self.backMsgType == 0) {
    NSString *contentStr = [NSString stringWithFormat:@"'%@'",content];
    NSDictionary *para = @{@"uid":uid,@"bid":self.backMsgId,@"content":contentStr};
    NSString *postUrl = [NSString stringWithFormat:@"%@/boat/boatfback",baseUrl];

    [SLNetWork postDataWithParameters:para postUrl:postUrl complete:^(id respond, NSError *error) {
      
      @strongify(self)
      if (!error) {
        [SVProgressHUD showSuccessWithStatus:@"信息反馈成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
      }else{
        [SVProgressHUD showSuccessWithStatus:@"信息反馈失败,请重试"];
      }
    }];
  }else{
    NSString *contentStr = [NSString stringWithFormat:@"'%@'",content];
    NSDictionary *para = @{@"uid":uid,@"gid":self.backMsgId,@"content":contentStr};

    [SLNetWork postDataWithParameters:para postUrl:[NSString stringWithFormat:@"%@/goods/goodsfback",baseUrl] complete:^(id respond, NSError *error) {
      @strongify(self)
      if (!error) {
        [SVProgressHUD showSuccessWithStatus:@"信息反馈成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
      }else{
        [SVProgressHUD showSuccessWithStatus:@"信息反馈失败,请重试"];
      }
    }];
  }
  
}




- (BOOL)vailToNext{
  if (self.noteButton.selected) {
    isNoteBtn = YES;
    if (self.noteTextView.text.length <= 0) {
      [SVProgressHUD showImage:nil status:@"请填写反馈信息"];
      return NO;
    }else if (self.noteTextView.text.length > 200){
      [SVProgressHUD showImage:nil status:@"备注信息长度限制为1-200"];
      return NO;
    }
    return YES;
  }
  
  isNoteBtn = NO;
  return YES;
}


- (void)colseAction{
  [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Public Methods

- (void)backBoatMsgWith:(NSString *)bid{
  self.backMsgType = 0;
  self.backMsgId = bid;
  self.firstLB.text = @"船东联系方式错误或无法取得联系。";
  self.thridLB.text = @"船舶基础信息有误，状态过期，位置错误。";
  self.secondLB.text = @"运价不明或未达成一致。";
  self.title = @"船舶反馈";
}

- (void)backGoodsMsgWith:(NSString *)hid{
  self.backMsgType = 1;
  self.backMsgId = hid;
  
  self.title = @"货源反馈";
  self.firstLB.text = @"货主联系方式错误或无法取得联系。";
  self.secondLB.text = @"货源信息有误，过期，不存在。";
  self.thridLB.text = @"货源运价不明或未达成一致。";
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (UIButton *)backButton{
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  [button setImage:[UIImage imageNamed:@"Not checked"] forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:@"Check"] forState:UIControlStateSelected];
  return button;
}

#pragma mark - getter

- (UIButton *)firstBtn{
  if (!_firstBtn) {
    _firstBtn = [self backButton];
    [_firstBtn addTarget:self action:@selector(choeseFirst:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _firstBtn;
}

- (UIButton *)secondBtn{
  if (!_secondBtn) {
    _secondBtn = [self backButton];
    [_secondBtn addTarget:self action:@selector(choeseSecond:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _secondBtn;
}

- (UIButton *)thridBtn{
  if (!_thridBtn) {
    _thridBtn = [self backButton];
    [_thridBtn addTarget:self action:@selector(choeseThird:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _thridBtn;
}


- (UIButton *)noteButton{
  if (!_noteButton) {
    _noteButton = [self backButton];
    [_noteButton addTarget:self action:@selector(choeseNote:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _noteButton;
}

- (UILabel *)firstLB{
  if (!_firstLB) {
    _firstLB = [UILabel labelWithFontSize:15 textColor:[UIColor blackColor]];
  }
  return _firstLB;
}

- (UILabel *)secondLB{
  if (!_secondLB) {
    _secondLB = [UILabel labelWithFontSize:15 textColor:[UIColor blackColor]];
  }
  return _secondLB;
}

- (UILabel *)thridLB{
  if (!_thridLB) {
    _thridLB = [UILabel labelWithFontSize:15 textColor:[UIColor blackColor]];
  }
  return _thridLB;
}

- (UITextView *)noteTextView{
  if (!_noteTextView) {
    _noteTextView = [[UITextView alloc] init];
    _noteTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _noteTextView.layer.borderWidth = 0.5f;
    _noteTextView.delegate = self;
    _noteTextView.editable = NO;
  }
  return _noteTextView;
}

- (UIButton *)confirmBtn{
  if (!_confirmBtn) {
    _confirmBtn = [UIButton buttonWithTitle:@"  提交  "];
    _confirmBtn.layer.cornerRadius = 5.f;
    [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _confirmBtn.backgroundColor = SLYellowColor;
    [_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
  }
  return _confirmBtn;
}

- (UIButton *)cancelBtn{
  if (!_cancelBtn) {
    _cancelBtn = [UIButton buttonWithTitle:@"  取消  "];
    _cancelBtn.layer.cornerRadius = 5.f;
    [_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _cancelBtn.backgroundColor = SLYellowColor;
    [_cancelBtn addTarget:self action:@selector(colseAction) forControlEvents:UIControlEventTouchUpInside];
  }
  return _cancelBtn;
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
